<template>
	<view>
	<u-modal v-model="show" :show-cancel-button="newVersionInfo.forceUpdateFlag==false"  
			title="发现新版本,更新吗?"
			cancel-text="以后升级"
			:confirm-text="newVersionInfo.forceUpdateFlag==false?'现在升级':'立即升级'"
			@cancel="cancel" @confirm="confirm">
		<view class="u-update-content">
			<rich-text :nodes="newVersionInfo.updateNote?newVersionInfo.updateNote:'修复已知bug'"></rich-text>
		</view>
	</u-modal>
	<view v-if="dowloading" :style="'height:'+windowHeight+'px'"  class="u-flex u-row-center u-col-center">
		<view>
			<u-circle-progress type="success" :percent="donwloadPercent" duration="2000">
				<view class="u-progress-content">
							<view class="u-progress-dot"></view>
							<text class='u-progress-info'>下载中{{donwloadPercent}}%</text>
				</view>
			</u-circle-progress>
		</view>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				windowHeight:uni.getSystemInfoSync().windowHeight,
				dowloading:false,
				donwloadPercent:0,
				newVersionInfo:{},
				show: false,
			}
		},
		onLoad(option) {
			if(option!=null&&option.newVersion!=null&&option.newVersion!=undefined&&option.newVersion.length>0){
				this.newVersionInfo=JSON.parse(option.newVersion);
				console.log("最新版本信息",this.newVersionInfo);
				this.show = true;
			}else{
				 this.globalUtil.utilAlert("页面加载失败!");
				 return;
			}
		},
		
		methods: {
			cancel() {
				this.toGroup();
			},
			confirm() {
				let that=this;
				let platfrom= that.$u.os();
				if(platfrom=="android"){
					let url=that.newVersionInfo.versionUrl;
					if(url==null||url==undefined|url.length<1){
						plus.nativeUI.toast('无安装包可用，请联系管理员');
						that.toGroup();
						return;
					}
					that.hotUpdateFun(url);
					return;
				}
				if(platfrom=="ios"){
					that.toAppStore();
					return;
				}
			},
			hotUpdateFun(url) {
			    let _this = this
			   _this.dowloading=true;
			   //var watiting = plus.nativeUI.showWaiting("开始下载：0%");
			    // 创建下载任务
			    const downloadTask = uni.downloadFile({
			        url: url,
			        success: (res) => {
			            if (res.statusCode === 200) {
			               // watiting.setTitle("安装中...");
			                plus.runtime.install(res.tempFilePath, {
			                    force: true
			                }, function (succ) {
								_this.$u.vuex("vuex_version",_this.newVersionInfo.versionName);
			                    //plus.nativeUI.closeWaiting();
								plus.runtime.restart();
								return;
			                }, function (e) {
			                    plus.nativeUI.closeWaiting();
			                    plus.nativeUI.alert('更新失败', function () {
			                        plus.runtime.openURL(url);
			                        plus.runtime.quit();
			                    });
			                });
			            }
			        },
					complete:function(){
						_this.dowloading=false;
					}
			    });
			    downloadTask.onProgressUpdate((res) => {
					 //watiting.setTitle("正在下载...");
					_this.donwloadPercent=res.progress;
			    });
			},
			
			toAppStore:function(){
				let that=this;
			   //#ifdef APP-PLUS
				//在App Store Connect中的App Store下的app信息，可找到appleId
				let appleId= 6476091723;
				plus.runtime.launchApplication({
					action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
				}, function(e) {
					console.log('Open system default browser failed: ' + e.message);
					that.toGroup();
				});
				return;
				//#endif
			},
				
			toGroup:function(){
				let that=this;
				let token=this.vuex_token;
				let userId=this.vuex_userInfo.id;
				//初始化腾讯音视频
				this.trcApi.TRCLogin(that);
				this.initTioWS(userId,"00000000",token);
				return;
			},
				
			initTioWS:function(userId,chatGroupId,token){
				let that=this;
				that.globalWX.closeWS();
				let param={
					userId:userId,
					chatGroupId:chatGroupId,
					token:token,
				}
				this.globalWX.initWebScoket(param);
				setTimeout(function(){
					uni.reLaunch({
						url:'/pages/tabbar/group/group'
					})
				},500);
			},
		}
	}
</script>

<style scoped lang="scss">
	.u-full-content {
		background-color: #00C777;
	}
	
	.u-update-content {
		font-size: 26rpx;
		color: $u-content-color;
		line-height: 1.7;
		padding: 30rpx;
	}
</style>
